﻿using System;
using System.Collections.Generic;

namespace YJKArch.AxisModule
{
	public static class CharStringAndGenericCodeConvert
	{
		public static int EnglishToGenericCode(string englishString)
		{
			List<int> list = new List<int>();
			int i = 0;
			int length = englishString.Length;
			while (i < length)
			{
				int num = CharAndNumberConvert.Chr2AscII(new string(englishString[i], 1));
				if (num != 73 && num != 79 && num >= 65 && num <= 90)
				{
					int item = CharStringAndGenericCodeConvert.m_s_CharAndGenericCodeMapping.CharToGenericCode(num);
					list.Add(item);
				}
				i++;
			}
			return CharStringAndGenericCodeConvert.BitListToGenericCode(list);
		}

		public static string GenericCodeToEnglish(int genericCode)
		{
			List<int> bitListFromGenericCode = CharStringAndGenericCodeConvert.GetBitListFromGenericCode(genericCode);
			string text = "";
			foreach (int num in bitListFromGenericCode)
			{
				if (num != -1)
				{
					string str = CharAndNumberConvert.AscII2Chr(CharStringAndGenericCodeConvert.m_s_CharAndGenericCodeMapping.GenericCodeToChar(num));
					text += str;
				}
			}
			return text;
		}

		public static int GeneticCodeAddition(int genericCode, int addend)
		{
			for (int i = 0; i < addend; i++)
			{
				genericCode = CharStringAndGenericCodeConvert.GeneticCodeAddition_One(genericCode);
			}
			return genericCode;
		}

		public static int GeneticCodeSubtraction(int genericCode, int subtrahend)
		{
			for (int i = 0; i < subtrahend; i++)
			{
				genericCode = CharStringAndGenericCodeConvert.GeneticCodeSubtraction_One(genericCode);
			}
			return genericCode;
		}

		private static int GeneticCodeAddition_One(int genericCode)
		{
			int ary = CharAndGenericCodeMapping.Ary();
			List<int> bitListFromGenericCode = CharStringAndGenericCodeConvert.GetBitListFromGenericCode(genericCode);
			int count = bitListFromGenericCode.Count;
			int num = 0;
			int item = 0;
			List<int> list = new List<int>();
			for (int i = count - 1; i >= 0; i--)
			{
				int num2 = bitListFromGenericCode[i] + num;
				if (i == count - 1)
				{
					num2++;
				}
				CharStringAndGenericCodeConvert.DecimalToGenericCodeAry(num2, ary, ref num, ref item);
				list.Insert(0, item);
			}
			if (num != 0)
			{
				int value = num;
				do
				{
					CharStringAndGenericCodeConvert.DecimalToGenericCodeAry(value, ary, ref num, ref item);
					list.Insert(0, item);
					value = num;
				}
				while (num != 0);
			}
			return CharStringAndGenericCodeConvert.BitListToGenericCode(list);
		}

		private static int GeneticCodeSubtraction_One(int genericCode)
		{
			if (genericCode == 1)
			{
				return 1;
			}
			int num = CharAndGenericCodeMapping.Ary();
			List<int> bitListFromGenericCode = CharStringAndGenericCodeConvert.GetBitListFromGenericCode(genericCode);
			int count = bitListFromGenericCode.Count;
			int num2 = 1;
			List<int> list = new List<int>();
			for (int i = count - 1; i >= 0; i--)
			{
				int num3 = bitListFromGenericCode[i];
				if (num3 == num2)
				{
					if (i == 0)
					{
						break;
					}
					num3 = num;
				}
				else
				{
					num3 -= num2;
					num2 = 0;
				}
				list.Insert(0, num3);
			}
			return CharStringAndGenericCodeConvert.BitListToGenericCode(list);
		}

		private static List<int> GetBitListFromGenericCode(int genericCode)
		{
			List<int> list = new List<int>();
			int ary = CharAndGenericCodeMapping.Ary();
			int value = genericCode;
			int num = 0;
			int item = 0;
			do
			{
				CharStringAndGenericCodeConvert.DecimalToGenericCodeAry(value, ary, ref num, ref item);
				list.Insert(0, item);
				value = num;
			}
			while (num != 0);
			return list;
		}

		private static void DecimalToGenericCodeAry(int value, int ary, ref int quotient, ref int remainder)
		{
			quotient = value / ary;
			remainder = value % ary;
			if (remainder == 0)
			{
				remainder = ary;
				quotient--;
			}
		}

		private static int BitListToGenericCode(List<int> bitList)
		{
			int num = CharAndGenericCodeMapping.Ary();
			int num2 = 0;
			int i = 0;
			int count = bitList.Count;
			while (i < count)
			{
				int num3 = bitList[i];
				num2 += num3 * (int)Math.Pow((double)num, (double)(count - i - 1));
				i++;
			}
			return num2;
		}

		private static CharAndGenericCodeMapping m_s_CharAndGenericCodeMapping = new CharAndGenericCodeMapping();
	}
}
